#include "precompiled.h"

#include <helpers/support/environment.h>
#include <QApplication>
#include <extensions/context.h>

#include <stdlib.h>
namespace Opl { namespace Helpers { namespace Support{

class EnvironmentPrivate{
public:
    EnvironmentPrivate() {}
    ~EnvironmentPrivate() {}
};

Environment* Environment::instance()
{
    static Environment* environment = 0;
    if (environment == 0)
        environment = new Environment(0);
    return environment;
}

Environment::Environment(Opl::Core::EntityObject *parent) :
    Opl::Core::EntityObject(parent), O_PRIVATE_CREATE(Environment)
{
}

Environment::~Environment()
{
    O_PRIVATE_DELETE;
}

QString Environment::username()
{
#if defined (Q_WS_WIN)
    return QString(getenv("USERNAME"));
#elif (Q_WS_X11)
    return QString getenv("USER"));
#endif
}

QString Environment::findFile(const QString& filename, SearchPathModes mode/* = all*/)
{
    O_D(Environment);
    QStringList paths = searchPath(mode);
    foreach (QString path, paths){
        QDir dir = QDir(path);
        QString fullPath = dir.absoluteFilePath(filename);
        if (QFile::exists(fullPath)){
            return fullPath;
        }
    }
    return QString();
}

QStringList Environment::searchPath(SearchPathModes mode)
{
    QStringList output;

    if (mode.testFlag(script_sdk_include)){
        QDir d(QCoreApplication::applicationDirPath());
        if (d.cd("extensions")){
            output << d.absolutePath();

            //			if (d.cd("include")){
            //				output << d.absolutePath();
            //			}
        }
    }

    //	if (mode.testFlag(binary)){
    //		output.append(QCoreApplication::applicationDirPath());
    //	}

    if (mode.testFlag(system_path)){
        QString PATH = QString(getenv("PATH"));
        output.append(PATH.split(';'));
    }

    return output;
}

QString Environment::expandPath(SearchPathMode mode, const QString& path)
{
    O_D(Environment);
    QStringList paths = searchPath(mode);
	if (paths.isEmpty())
		return QString();

    QDir dir(paths.first());
    return dir.absolutePath() + "/" + path;
}

QString Environment::expandPath(const QString path, const Optra::Extensions::Context* context)
{
    Q_ASSERT(context);

    if (context == 0)
        return path;

    QString out = path;
    out.replace("$BINDIR", context->binDir());
    out.replace("$ROOT", context->rootDir().absolutePath());
    out.replace("$PATH", QString(getenv("PATH")));
    out.replace("$ResourcesDir",  context->resourcesDir());
    return out;
}

QDir Environment::getDirectoryLocation(DirectoryKind dir)
{
    switch(dir){
    case UserDirectory:
        QDir::home();
        break;

    case DocumentsDirectory:{
#if defined (Q_WS_WIN)
        QSettings settings(QSettings::UserScope, "Microsoft", "Windows");
        settings.beginGroup("CurrentVersion/Explorer/Shell Folders");
        return settings.value("Personal").toString();
#elif (Q_WS_X11)
        //TODO: Test this
        return getDirectoryLocation(UserDirectory).absoluteFilePath("Documents");
#endif
    };break;

    }

    return QDir();
}

}}}
